package com.example.demos.util;



import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SendMsg {

    // 签名
    private static final String signName = "码上未来ERP管理系统";

    // 模板
    private static final String templateCode = "SMS_252280037";

    // 阿里云短信配置信息
    private static final String accessKeyId = "LTAI5t6frzQvZ2jfnWyaWtpP";
    private static final String accessKeySecret = "AwNOX2m1mSkmuivBk5CHqt0zAuVew1";
    private static final String REGION_ID = "cn-hangzhou";
    private static final String PRODUCT = "Dysmsapi";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";


    public static boolean sendMessage(String tel) {
        try {
            IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);

            DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);

            IAcsClient acsClient = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();

            request.setMethod(MethodType.POST);

            // 手机号可以单个也可以多个（多个用逗号隔开，如：15*******13,13*******27,17*******56）
            request.setPhoneNumbers(tel);

            request.setSignName(signName);

            request.setTemplateCode(templateCode);


            request.setTemplateParam("{\"data\":\"仓库\"}");

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if ((sendSmsResponse.getCode() != null) && (sendSmsResponse.getCode().equals("OK"))) {
//                log.info("发送成功,code:" + sendSmsResponse.getCode());
                return true;
            } else {
                System.out.println("发送失败,code:" + sendSmsResponse.getCode());
//                log.info("发送失败,code:" + sendSmsResponse.getCode());
                return false;
            }
        } catch (ClientException e) {
            throw new RuntimeException(e);
//            log.error("发送失败,系统错误！", e);

        }}}
